﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BarberShop.BLL;


public partial class MoviePlaySortAdd : BasePage
{
    public int Id
    {
        get
        {
            return string.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"]);
        }
    }
    public IList<int> MoveIds
    {
        get
        {
            var list = MovePlayListRelationBLL.GetMoviePalyListRe(Id);
            var r = (from a in list select a.MoveInfo_ID.Value).ToList();
            return r;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetMovieData();
        }
    }

    protected void btn_search_Click(object sender, EventArgs e)
    {
        GetMovieData();
    }

    //getData
    protected void GetMovieData()
    {
        string name = tx_movieName.Text.Trim();
        var data = MovieInfoBLL.GetMovieList(name);
        this.Grid_Movie.DataSource = data;
        this.Grid_Movie.DataKeyNames = new string[] { "ID" };
        this.Grid_Movie.DataBind();
    }

    protected void Grid_Movie_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            double size = Convert.ToDouble(e.Row.Cells[5].Text.ToString());
            CheckBox cbox = (CheckBox)e.Row.Cells[0].FindControl("CheckBox1");
            if (MoveIds.Contains((int)Grid_Movie.DataKeys[e.Row.RowIndex].Value))
            {
                cbox.Checked = true;
                cbox.Enabled = false;
            }
            string sizename = "";
            if (size / 1024 < 1024)
            {
                sizename = (size / (double)1024).ToString("N2") + "Kb";
            }
            else if (size / 1024 / 1024 < 1024)
            {
                sizename = (size / (double)1024 / (double)1024).ToString("N2") + "Mb";
            }
            else
            {
                sizename = (size / (double)1024 / (double)1024 / (double)1024).ToString("N2") + "Gb";
            }
            e.Row.Cells[5].Text = sizename;

            int timelength = Convert.ToInt32(e.Row.Cells[6].Text.ToString());
            e.Row.Cells[6].Text = new TimeSpan(0, 0, timelength).ToString();
        }
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        string mids = "";
        for (int i = 0; i <= Grid_Movie.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)Grid_Movie.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true && cbox.Enabled == true)
            {
                int mid = (int)Grid_Movie.DataKeys[i].Value;
                mids += mid.ToString() + ",";
            }

        }
        MovePlayListRelationBLL.AddPalyList(Id, mids);
        GetMovieData();
    }
    protected void Grid_Movie_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.Grid_Movie.PageIndex = e.NewPageIndex;
    }
}
